package io.gitee.mingbaobaba.apijson.querycondition.query.conditions;

import io.gitee.mingbaobaba.apijson.querycondition.query.exception.ConditionException;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * <p>基于String形式构建查询参数</p>
 *
 * @author yingsheng.ye
 * @version 1.0.0
 * @since 2023/6/19 21:40
 */
public class ApiJsonQueryStringWrapper<T> extends AbstractQueryWrapper<T, String, ApiJsonQueryStringWrapper<T>> implements ApiJsonQuery {


    /**
     * 添加条件
     *
     * @param condition 条件false忽略
     * @param column    列
     * @param keyword   连接条件
     * @param val       值
     * @return ApiJsonQueryStringWrapper<T>
     */
    @Override
    protected ApiJsonQueryStringWrapper<T> addCondition(boolean condition, String column, EnumKeyword keyword, Object val) {
        if (condition) {
            //条件
            this.conditionHandle(new Condition(column, keyword, val));
        }
        return typedThis;
    }


    /**
     * 添加聚合函数
     *
     * @param condition 条件false忽略
     * @param column    列
     * @param keyword   连接条件
     * @param val       值
     * @return ApiJsonQueryStringWrapper
     */
    @Override
    protected ApiJsonQueryStringWrapper<T> addAggFunc(boolean condition, String column, EnumKeyword keyword, Object val) {
        if (condition) {
            this.aggFuncHandle(keyword, column, val);
        }
        return typedThis;
    }

    /**
     * 添加分组函数
     *
     * @param condition 条件false忽略
     * @param column    列
     * @param keyword   连接条件
     * @return ApiJsonQueryStringWrapper
     */
    @Override
    protected ApiJsonQueryStringWrapper<T> addGroupFunc(boolean condition, String column, EnumKeyword keyword) {
        if (condition && keyword.equals(EnumKeyword.GROUP)) {
            this.groupFuncHandle(keyword, column);
        }
        return typedThis;
    }

    /**
     * 添加排序
     *
     * @param condition 条件
     * @param column    列
     * @param keyword   关键字
     * @param s         排序值 +升序 -降序
     * @return ApiJsonQueryStringWrapper
     */
    @Override
    protected ApiJsonQueryStringWrapper<T> addOrderByFunc(boolean condition, String column, EnumKeyword keyword, String s) {
        if (condition && keyword.equals(EnumKeyword.ORDER_BY)) {
            this.orderByFuncHandle(keyword, column + s);
        }
        return typedThis;
    }

    /**
     * 实例化对象
     *
     * @return ApiJsonQueryStringWrapper
     */
    @Override
    protected ApiJsonQueryStringWrapper<T> instance() {
        return new ApiJsonQueryStringWrapper<>();
    }


    /**
     * join关联
     *
     * @param tableName 关联表名
     * @param column    列名
     * @param consumer  Consumer
     * @param <E>       泛型
     * @return ApiJsonQueryStringWrapper<T>
     */
    public <E> ApiJsonQueryStringWrapper<T> join(Class<E> tableName, String column,
                                                 String relColumn, Consumer<JoinWrapper<E>> consumer) {
        if (Objects.nonNull(joinOperation)) {
            throw new ConditionException("join只支持单个配置");
        }
        final JoinWrapper<E> instance = joinInstance();
        instance.setColumn(column);
        instance.setRelColumn(relColumn);
        instance.setTableName(tableName);
        if (null != consumer) {
            consumer.accept(instance);
        }
        joinOperation = instance;
        return typedThis;
    }

    /**
     * join关联
     *
     * @param tableName 表名
     * @param column    列名
     * @param relColumn 关联列名
     * @param <E>       泛型
     * @return ApiJsonQueryLambdaWrapper<T>
     */
    public <E> ApiJsonQueryStringWrapper<T> join(Class<E> tableName, String column, String relColumn) {
        return join(tableName, column, relColumn, null);
    }


    /**
     * join实例
     *
     * @param <E> 对象类型
     * @return JoinWrapper<E>
     */
    @Override
    protected <E> JoinWrapper<E> joinInstance() {
        return new JoinWrapper<>();
    }

    /**
     * select
     *
     * @param columns 查询列
     * @return ApiJsonQueryStringWrapper<T>
     */
    @Override
    public ApiJsonQueryStringWrapper<T> select(String... columns) {
        return select(true, columns);
    }

    /**
     * select
     *
     * @param condition 条件false忽略
     * @param columns   查询列
     * @return ApiJsonQueryStringWrapper<T>
     */
    @Override
    public ApiJsonQueryStringWrapper<T> select(boolean condition, String... columns) {
        if (condition) {
            columnList.addAll(Arrays.asList(columns));
        }
        return typedThis;
    }

}

